package lec;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class A implements Serializable {
	public int x = 10;
	private String y = "Hello";
	public String toString(){return "" + x + " " +y;}
}
public class serialize {
	public static void main(String[] args){
		A a = new A();
		a.x = 1;

		try {
			FileOutputStream fout = new FileOutputStream("MyObject");
			ObjectOutputStream oos = new ObjectOutputStream( fout );
			oos.writeObject( a );
			oos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		A b = null;
		try {
			FileInputStream fin = new FileInputStream("MyObject");
			ObjectInputStream ois = new ObjectInputStream( fin );
			b = (A) ois.readObject();
			ois.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

		System.out.println( "Read from a file: " + b);
	}
}
